package com.yanqu.road.server.logic.thread;

import com.yanqu.road.server.helper.SignHelper;
import com.yanqu.road.server.manger.AppKeyMgr;
import com.yanqu.road.utils.executor.GeneralTask;
import com.yanqu.road.utils.http.HttpHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

public class RoleReportTask extends GeneralTask {
    private Logger logger = LogManager.getLogger(RoleReportTask.class.getName());

    private String userID;
    private String serverID;
    private String actorID;
    private String actorName;
    private String userIp;

    public RoleReportTask(String userID, String serverID, String actorID, String actorName, String userIp) {
        super();
        this.userID = userID;
        this.serverID = serverID;
        this.actorID = actorID;
        this.actorName = actorName;
        this.userIp = userIp;
    }

    @Override
    public void doRun() {
        SortedMap<String, String> paramMap = new TreeMap<>();
        try {
            paramMap.put("userID", userID);
            paramMap.put("serverID", serverID);
            paramMap.put("actorID", actorID);
            paramMap.put("actorName", URLEncoder.encode(actorName, "UTF-8"));
            paramMap.put("userIp", userIp);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String sign = SignHelper.getSign(paramMap, AppKeyMgr.getAndroidAppSecret());
        paramMap.put("sign", sign);
        try {
            String url = AppKeyMgr.getPlatformBaseUrl() + "/chat/roleReporting/" + AppKeyMgr.getAndroidAppId() + "/" + AppKeyMgr.getPlatformChannelId();
            logger.info("RoleReport url:{}", url);
            Map<String, String> resultMap = HttpHelper.doGet(url, paramMap);
            if(null == resultMap){
                logger.info("RoleReport request error");
            }else {
                logger.info("RoleReport code:{}; result:{}", resultMap.get("code"), resultMap.get("body"));
            }
        } catch (Exception e) {
            logger.error("RoleReport error:",e);
        }
    }
}
